package com.lagou.edu.server;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 胡益文
 * @since 2021/9/28 19:58
 */
public class Request {
    private String method ;//根据方式， GET/POST
    private String url ; // 获取请求头中的url

    private String host ;//根据方式， host

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    private InputStream inputStream;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count =0;
        while (count==0){
            count = this.inputStream.available();
        }
        int available = this.inputStream.available();
        byte[] bytes = new byte[available];
        this.inputStream.read(bytes);
        String request = new String(bytes);
        System.out.println("请求信息为："+request);
        String[] split = request.split("\n");
        String firstLine = split[0];
        String[] firstLineArray = firstLine.split(" ");
        this.method = firstLineArray[0];
        this.url = firstLineArray[1];
        this.host = split[1].replace("Host:","").replace("\r","").trim();
    }

    @Override
    public String toString() {
        return "Request{" +
                "url='" + url + '\'' +
                ", method='" + method + '\'' +
                ", inputStream=" + inputStream +
                '}';
    }
}
